﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using System.Configuration;
using System.Data.SqlClient;


namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class Add_DetailsDrug : Form
    {
        E_DugType dr = new E_DugType();
        E_DrugDetails ar = new E_DrugDetails();
        errordrugdetails err;
        public Add_DetailsDrug()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AutomaticIncreaseID();
            ar.DrugID = Convert.ToInt32(txtdrugid.Text);
            ar.DrugTypeID = Convert.ToInt32(cbx_DrugType.Text.ToString());
            ar.NameDrug = tbx_DrugDetailsName.Text;
            ar.Unit = txtunit.Text;
            ar.Price = txtprice.Text;
            ar.Source = txtsource.Text;
            ar.Quantity = txtquantity.Text;
            ar.Productdate = Convert.ToDateTime(dateTimeprodutiongdate.Text);
            ar.UseTime = Convert.ToDateTime(dateTimeusetime.Text);
            ar.AddedWareHouseDate = Convert.ToDateTime(dateTimeaddedware.Text);
            ar.UseGuide = txtuseguide.Text;
            err = BL_E_DrugDetails.Adddrug(ar);
            switch (err)
            {
                case errordrugdetails.NoError:
                    MessageBox.Show("DrugDetails is added successfully!");
                    display();
                    break;
                case errordrugdetails.ErrorEmpty:
                    errorProvider1.SetError(tbx_DrugDetailsName, "Tên thuốc là rỗng");
                    break;
                case errordrugdetails.SqlError:
                    MessageBox.Show("Lỗi thực hiện thêm chi tiết thuốc !", "Lỗi thực hiện", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    break;
                default:
                    break;
            }
           

        }
        private void display()
        {
            dataGridView1.DataSource = BL_E_DrugDetails.RefressDrug();
        }

        private void Add_DetailsDrug_Load(object sender, EventArgs e)
        {
            cbx_DrugType.SelectedIndex = 0;
            display();
            display1();
        }
        private void clearfield()
        {
            tbx_DrugDetailsName.Text = "";
            txtunit.Text = "";
            txtprice.Text = "";
            txtquantity.Text = "";
            txtsource.Text = "";
            txtuseguide.Text = "";
        }
        private void clearfielddrugtype()
        {
            txtdrugtypeid1.Text = "";
            txtNametypedrugid2.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clearfielddrugtype();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clearfield();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult traloi;
            traloi = MessageBox.Show("bạn chắc chán ko muốn lưu lại?", "chắn", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(traloi==DialogResult.OK)
            {
                clearfield();
            }
        }

      
        private void AutomaticIncreaseID()
        {
            int i = BL_E_DrugDetails.CountDrug;
            string Sodau ="0";
            if (i < 9)
            {
               
                Sodau = Sodau + "0";
            }
           
           if (i > 9 && i <= 99)
            {
                Sodau = Sodau + "00";
            }

            string k = Sodau + i.ToString();
            txtdrugid.Text = k;
           
        }
        private void AutomaticIncreaseIDType()
        {
            int i = BL_E_DugType.CountDrugType;
            string Sodau = "0";
            if (i < 9)
            {

                Sodau = Sodau + "0";
            }

            if (i > 9 && i <= 99)
            {
                Sodau = Sodau + "00";
            }

            string k = Sodau + i.ToString();
            txtdrugtypeid1.Text = k;
           
        
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            cbx_DrugType.DataSource = BL_E_DugType.DrugType();
            cbx_DrugType.ValueMember = "DrugTypeID";
            cbx_DrugType.DisplayMember = "DrugTypeName";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtdrugid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbx_DrugDetailsName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtunit.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            txtprice.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            txtquantity.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            txtsource.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            dateTimeprodutiongdate.Value = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[6].Value);
            dateTimeusetime.Value = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[7].Value);
            txtuseguide.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
            dateTimeaddedware.Value = Convert.ToDateTime(dataGridView1.CurrentRow.Cells[9].Value);
            cbx_DrugType.Text = dataGridView1.CurrentRow.Cells[10].Value.ToString();
        }

        private void btnaddtype_Click_1(object sender, EventArgs e)
        {
            AutomaticIncreaseIDType();
            dr.DrugTypeID1 = Convert.ToInt32(txtdrugtypeid1.Text);
            dr.NameDrugType = txtNametypedrugid2.Text;
            err = BL_E_DrugDetails.adddrugtype(dr);
            switch (err)
            {
                case errordrugdetails.NoError:

                    MessageBox.Show("TypeDrug is added successfully!");
                    display1();
                    break;
                case errordrugdetails.ErrorEmpty:
                    MessageBox.Show("TypeDrug is added faild!");
                    break;
                default:
                    break;

            }

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void display1()
        {
            dataGridView2.DataSource = BL_E_DrugDetails.Refressh();
        }
    }
}
